package net.diaowen.dwsurvey.service.impl;

import net.diaowen.common.service.BaseServiceImpl;
import net.diaowen.common.utils.excel.XLSExportUtil;
import net.diaowen.dwsurvey.dao.UserGroupInfoDao;
import net.diaowen.dwsurvey.entity.SurveyDirectory;
import net.diaowen.dwsurvey.entity.UserGroupInfo;
import net.diaowen.dwsurvey.service.UserGroupInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @IntelliJ IDEA
 * @刘圣禹
 * @2021/5/25
 */
@Service
public class UserGroupInfoManagerImpI extends BaseServiceImpl<UserGroupInfo,String> implements UserGroupInfoManager {

    @Autowired
    private UserGroupInfoDao userGroupInfoDao;
    @Override
    public void setBaseDao() {
        this.baseDao=userGroupInfoDao;
    }
    @Override
    public List<UserGroupInfo> all(){
        return userGroupInfoDao.getAll();
    }

    public List<UserGroupInfo> GroupQuery(String orgid){
        return userGroupInfoDao.findBy("orgId",orgid);
    }

    public List<Object[]> QueryAllUnanswered(String surveyId){
        String sql1="select a.groupId from SurveyGroup a \n" +
                ", UserGroupInfo b \n" +
                "where b.id=a.groupId and a.surveyId= ? ";
        String sql="select a.name ,a.college ,a.major , a.studentId  from User a, UserGroup b\n" +
                "where a.id = b.userId and b.usergroupId in ("+sql1+") \n" +
                "and a.id not in \n" +
                "( select userId from SurveyAnswer where surveyId= ? and userId is not null) and a.studentId is not null";
        return userGroupInfoDao.findList(sql,surveyId,surveyId);
    }


    public String exportXLS(String surveyId, String savePath){
        String basepath = surveyId + "";
        String urlPath = "/WEB-INF/expfile/" + basepath + "/";// 下载所用的地址
        String path = urlPath.replace("/", File.separator);// 文件系统路径
        savePath = savePath + path;
        File file = new File(savePath);
        if (!file.exists())
            file.mkdirs();

        String fileName = "unansweredSurvey-"+surveyId + ".xls";

        XLSExportUtil exportUtil = new XLSExportUtil(fileName, savePath);

        try{
            List<Object[]> list = QueryAllUnanswered(surveyId);
            exportXLSTitle(exportUtil);
            int List_Size=list.size();

            for (int i = 0; i < List_Size; i++) {

                Object[] unansweredArray= list.get(i);

                exportUtil.createRow(i+1);

                exportXLSRow(exportUtil,unansweredArray);

                System.out.println(i+1+"/"+List_Size);
            }
            exportUtil.exportXLS();


        }catch (Exception e){
            e.printStackTrace();
        }


        return urlPath + fileName;
    }

    private void exportXLSTitle(XLSExportUtil exportUtil){
        exportUtil.createRow(0);
        int cellIndex = 0;
        exportUtil.setCell(cellIndex++,  "姓名");
        exportUtil.setCell(cellIndex++,  "学院");
        exportUtil.setCell(cellIndex++,  "专业");
        exportUtil.setCell(cellIndex++,  "学号");
    }

    private void exportXLSRow(XLSExportUtil exportUtil,Object[] unansweredArray){
        int cellIndex = 0;
        for (int i = 0; i < unansweredArray.length; i++) {
            exportUtil.setCell(cellIndex++, (String) unansweredArray[i]);
        }

    }
}
